home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / bookmark_previews-0.6.5-fx.xpi / chrome / content / previewServiceUtils.js < prev    next >
Text File  |  2008-05-27  |  2KB  |  45 lines

  1. var PreviewServiceUtils = {
  2.   hashString : function(str){
  3.     var converter =
  4.       Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
  5.         createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  6.  
  7.     // we use UTF-8 here, you can choose other encodings.
  8.     converter.charset = "UTF-8";
  9.     var result = {};
  10.     // data is an array of bytes
  11.     var data = converter.convertToByteArray(str, result);
  12.     var ch = Components.classes["@mozilla.org/security/hash;1"]
  13.                        .createInstance(Components.interfaces.nsICryptoHash);
  14.     ch.init(ch.SHA1);
  15.     ch.update(data, data.length);
  16.     var hash = ch.finish(false);
  17.  
  18.     // return the two-digit hexadecimal code for a byte
  19.     function toHexString(charCode)
  20.     {
  21.       return ("0" + charCode.toString(16)).slice(-2);
  22.     }
  23.  
  24.     // convert the binary hash data to a hex string.
  25.     var s = [toHexString(hash.charCodeAt(i)) for (i in hash)].join("")
  26.     return s;
  27.   },
  28.   getPreviewFile : function(aURL,dir){
  29.     if (!dir){
  30.       dir = Components.classes["@mozilla.org/file/directory_service;1"]
  31.                      .getService(Components.interfaces.nsIProperties)
  32.                      .get("ProfD", Components.interfaces.nsIFile);
  33.       dir.append("bookmarkpreviews");
  34.     }
  35.     var file = dir.clone();
  36.     var filename = this.hashString(aURL.toLowerCase()).substring(0,40);
  37.     file.append(filename);
  38.     //dump("getpreviewfile: "+aURL+"|"+aURL.length+"\n"+
  39.     //     "filename: "+filename+"\n"+
  40.     //     "path:     "+file.leafName+"  |  "+filename.length+"\n"+
  41.     //     "exists: "+file.exists()+"\n");
  42.     return file;
  43.   }
  44. }
  45.